Sub Statement

Declares the name, parameters, and code that form the body of a subroutine (method).


Syntax

Sub name([parameterList])

[local variable declarations]

[statements]

[ Return]

[statements]

[exception handlers]

[ Finally]

PartDescription
name Required. The name of the subroutine (method); follows standard variable naming conventions.
parameterList Optional. List of values representing parameters that are passed to the Subroutine when it's called. Multiple parameters are separated by commas.


Notes

The Sub statement is used to define a method. The word Sub is used because a subroutine is the equivalent of a method in the BASIC language. Methods are usually associated with an object (exceptions are global methods or functions that are part of a module). All executable code must be in a Sub or Function statement. A Sub can not be defined inside another Sub or Function. A Sub executes each line of code from the top down, assuming that you have not used the Goto statement. Once the last line of code is executed, control returns to the line following the statement that called the Sub.

You call a Sub by using its name followed by any parameters in parentheses.

Variables used in a Sub can be global, public, protected, private, or local in scope. Global variables can be accessed from within any Sub or Function. They exist from the moment the application runs to the time it quits. Global variables are created by creating properties of a module and declaring their scope Global. Public variables (a.k.a. properties) work like global properties except that the name of the owning window, class, or module must be used when referring to them, e.g., "module1.publicProperty". Private and protected properties are available only within the owning object and are called using the name of the owning window, class, or module.

Local variables are variables that are created each time the Sub is run and destroyed when the Sub finishes. Consequently, they can only be accessed by the statements within the Sub. They are created by using the Dim statement from within a Sub or Function. A Dim statement can appear anywhere within the Sub.

The Return statement can be used to immediately return control to the statement that called the Sub.

Exception handlers are statements that handle runtime errors. See the RuntimeException class and the Exception and Try blocks for more information.

Sometimes a method needs to do some cleanup work whether it is finishing normally or aborting early because of an exception. The optional Finally block at the end of the method runs after the exception handlers, if it has any. Code in this block will be executed even if an exception has occured, whether the exception was handled or not.

If you need your method to return a value, you will want to declare it as a Function. A Function is a method that can return a value. The value can be a one-dimensional array. See the Function statement for more information.

By default, parameter passing is done by value and the value cannot be modified by the method. You can also pass a parameter by reference. When you pass a parameter by reference, a pointer to the object is passed. This allows you to return a value using the parameter.

If you want to pass a parameter by reference, precede it by the keyword ' ByRef' in the parameter list, e.g., ' ByRef MyInt as Integer').

A Sub method can call itself resulting in recursion. Too much recursion can lead to StackOverFlowException errors.


See Also

Break, Catch, Exit, Finally, Function, Return statements; RuntimeException class; Exception, Try blocks.